package com.atguigu.realtime.utils

import java.time.{Instant, LocalDateTime, ZoneId}
import java.time.format.DateTimeFormatter

/**
 * Created by Smexy on 2022/8/26
 */
object DateTimeUtil {

  val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
  val formatter4: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
  val formatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
  val formatter3: DateTimeFormatter = DateTimeFormatter.ofPattern("HH")

  def parseMillTsToDateTime(ts:Long):String ={

    LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Shanghai")).format(formatter1)

  }

  def parseMillTsToDate(ts:Long):String ={

    LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Shanghai")).format(formatter2)

  }

  def parseMillTsToDateTimeMinute(ts:Long):String ={

    LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Shanghai")).format(formatter4)

  }

  // "create_time": "2022-08-26 13:21:35",  解析为 时间和小时
  def parseTimeStrToDate(timeStr : String):String ={

    LocalDateTime.parse(timeStr,formatter1).format(formatter2)

  }

  def parseTimeStrToHour(timeStr : String):String ={

    LocalDateTime.parse(timeStr,formatter1).format(formatter3)

  }

  def main(args: Array[String]): Unit = {

    var timeStr ="2022-08-26 13:21:35"
    println(DateTimeUtil.parseTimeStrToDate(timeStr))
    println(DateTimeUtil.parseTimeStrToHour(timeStr))

  }

}
